<?php

namespace addons\booking\controller;

use addons\booking\model\Collect as CollectModel;

class Collect extends Base
{
    protected $noNeedLogin = [];

    /**
     * 添加/取消收藏
     */
    public function optionCollect()
    {
        $type = $this->request->param('type/d');
        $source_id = $this->request->param('source_id');
        if (!$this->request->isPost()) {
            $this->error('请求错误');
        }
        if (!$source_id) {
            $this->error('缺少参数！');
        }
        $typeStr = [
            '\addons\booking\model\House',
            '\addons\booking\model\Goods',
        ];
        if (!isset($typeStr[$type])) {
            $this->error('类型不存在');
        }
        $data = (new $typeStr[$type]())->get($source_id);
        if (!$data) {
            $this->error('收藏的商品已失效！');
        }
        $status = CollectModel::addOrCancel($this->auth->id, $type, $source_id);
        if ($status) {
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    /**
     * 收藏列表
     */
    public function collectList()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->auth->id;
        $this->success('', CollectModel::tableList($param));
    }
}
